import UIKit

public class DWRouterModify {
    
    /// 模块的标识符
    let identifier:String
    
//    let block:RouterBlock?
    
    let type : DWRouterModule.Type
    
    var parame : Any?
    
//    internal typealias RouterBlock = ((_ modify:DLRouterModify) -> DLRouterModule?)
    
    init(identifier:String, type: DWRouterModule.Type) {
        self.identifier = identifier
//        self.block = block
        self.type = type
    }
    
    /// 获取前往模块的对象控制器
    /// - Returns: 控制器对象
    private func controller() -> UIViewController? {
        if let cls = DWRouterModify.classFromString(self.identifier) {
            if cls is UIViewController.Type {
                let controller = (cls as! UIViewController.Type).init()
                return controller
            }
            return nil
        }else {
            return nil
        }
    }
    
    /// 字符串转类
    class func classFromString(_ className:String) -> AnyClass? {
        // 1、获swift中的命名空间名
        var name = Bundle.main.object(forInfoDictionaryKey: "CFBundleExecutable") as? String
        // 2、如果包名中有'-'横线这样的字符，在拿到包名后，还需要把包名的'-'转换成'_'下横线
        name = name?.replacingOccurrences(of: "-", with: "_")
        // 3、拼接命名空间和类名，”包名.类名“
        let fullClassName = name! + "." + className
        // 通过NSClassFromString获取到最终的类
        let anyClass: AnyClass? = NSClassFromString(fullClassName)
        // 本类type
        return anyClass
    }
    
    /// 模态弹出
    /// - Parameters:
    ///   - presentController: 需要执行模态弹出的控制器
    ///   - animated: 是否有动画
    ///   - completion: 执行完毕动画
    public func present(in presentController:UIViewController?, animated:Bool, completion:(() -> Void)?) {
        guard let viewController = self.controller() else {
            return
        }
//        if let modalPresentationStyle = self.modalPresentationStyle {
//            viewController.modalPresentationStyle = modalPresentationStyle
//        }
        
        presentController?.present(viewController, animated: animated, completion: completion)
    }
    
    func copy() -> DWRouterModify {
        return DWRouterModify(identifier: self.identifier, type: self.type)
    }
    
}
